package com.trailwalker.javafx.utils.style;

import com.trailwalker.javafx.utils.screen.ScreenUtils;
import javafx.geometry.Insets;
import javafx.scene.Node;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.Region;
import javafx.stage.Screen;

/**
 * @author Trailwalker
 * @create 2021-09-05 21:15
 */
public class AdaptUtils {

    private static final String TAG = AdaptUtils.class.getSimpleName();

    private static AdaptUtils adaptUtils;
    private final ScreenUtils mScreenUtils;

    private AdaptUtils() {
        mScreenUtils = ScreenUtils.getInstance();
    }

    public static AdaptUtils getInstance() {
        if (adaptUtils == null) {
            adaptUtils = new AdaptUtils();
        }
        return adaptUtils;
    }

    public void setMatchParentAnchorPane(Node node) {
        AnchorPane.setTopAnchor(node, 0.0);
        AnchorPane.setRightAnchor(node, 0.0);
        AnchorPane.setBottomAnchor(node, 0.0);
        AnchorPane.setLeftAnchor(node, 0.0);
    }

    private double getScale() {
        double screenHeight = mScreenUtils.getHeight();
        return screenHeight / 1080;
    }

    public double getScale(double scale) {
        return scale * getScale();
    }

    public void setScaleHeight(Region region, double scaleHeight) {
        region.setPrefHeight(scaleHeight * getScale());
        region.setMaxHeight(scaleHeight * getScale());
    }

    public void setScaleWidth(Region region, double scaleWidth) {
        region.setPrefWidth(scaleWidth * getScale());
        region.setMaxWidth(scaleWidth * getScale());
    }

    public void setScaleSize(Region region, double scaleWidth, double scaleHeight) {
        setScaleHeight(region, scaleHeight);
        setScaleWidth(region, scaleWidth);
    }

    public void setScaleSize(Region region, double scale) {
        setScaleHeight(region, scale);
        setScaleWidth(region, scale);
    }

    public void setScalePadding(Region region, double scalePadding) {
        region.setPadding(new Insets(getScale(scalePadding)));
    }

    public void setScalePaddingLeft(Region region, double scalePadding) {
        Insets padding = region.getPadding();
        region.setPadding(new Insets(padding.getTop(), padding.getRight(), padding.getBottom(), scalePadding));
    }

    public void setScalePaddingRight(Region region, double scalePadding) {
        Insets padding = region.getPadding();
        region.setPadding(new Insets(padding.getTop(), scalePadding, padding.getBottom(), padding.getLeft()));
    }

    public void setScalePaddingLeftAndRight(Region region, double scalePadding) {
        Insets padding = region.getPadding();
        region.setPadding(new Insets(padding.getTop(), scalePadding, padding.getBottom(), scalePadding));
    }

    public void setScalePaddingTop(Region region, double scalePadding) {
        Insets padding = region.getPadding();
        region.setPadding(new Insets(scalePadding, padding.getRight(), padding.getBottom(), padding.getLeft()));
    }

    public void setScalePaddingBottom(Region region, double scalePadding) {
        Insets padding = region.getPadding();
        region.setPadding(new Insets(padding.getTop(), padding.getRight(), scalePadding, padding.getLeft()));
    }

    public void setScalePaddingTopAndBottom(Region region, double scalePadding) {
        Insets padding = region.getPadding();
        region.setPadding(new Insets(scalePadding, padding.getRight(), scalePadding, padding.getLeft()));
    }

    public void setScaleTextSize(Node node, double scaleFontSize) {
        node.setStyle(node.getStyle() + "-fx-font-size: " + getScale(scaleFontSize) + ";");
    }

    public Insets getMarginLeft(double marginLeftScale, Insets insets) {
        if (insets == null) {
            insets = new Insets(0, 0, 0, 0);
        }
        return new Insets(insets.getTop(), insets.getRight(), insets.getBottom(), getScale(marginLeftScale));
    }

    public Insets getMarginRight(double marginRightScale, Insets insets) {
        if (insets == null) {
            insets = new Insets(0, 0, 0, 0);
        }
        return new Insets(insets.getTop(), getScale(marginRightScale), insets.getBottom(), insets.getLeft());
    }

    public Insets getMarginLeftAndRight(double marginLeftAndRightScale, Insets insets) {
        if (insets == null) {
            insets = new Insets(0, 0, 0, 0);
        }
        return new Insets(insets.getTop(), getScale(marginLeftAndRightScale), insets.getBottom(), getScale(marginLeftAndRightScale));
    }

    public Insets getMarginTop(double marginTopScale, Insets insets) {
        if (insets == null) {
            insets = new Insets(0, 0, 0, 0);
        }
        return new Insets(getScale(marginTopScale), insets.getRight(), insets.getBottom(), insets.getLeft());
    }

    public Insets getMarginBottom(double marginBottomScale, Insets insets) {
        if (insets == null) {
            insets = new Insets(0, 0, 0, 0);
        }
        return new Insets(insets.getTop(), insets.getRight(), getScale(marginBottomScale), insets.getLeft());
    }

    public Insets getMarginTopAndBottom(double marginTopAndBottomScale, Insets insets) {
        if (insets == null) {
            insets = new Insets(0, 0, 0, 0);
        }
        return new Insets(getScale(marginTopAndBottomScale), insets.getRight(), getScale(marginTopAndBottomScale), insets.getLeft());
    }

    public Insets getMargin(double marginScale) {
        return new Insets(getScale(marginScale));
    }

    public void setBackgroundRadius(Node node, int scaleRadius) {
        node.setStyle(node.getStyle() + "-fx-background-radius:" + getScale(scaleRadius) + "px;");
    }

    public void setBorderRadius(Node node, int scaleRadius) {
        node.setStyle(node.getStyle() + "-fx-border-radius:" + getScale(scaleRadius) + "px;");
    }

}
